package cn.yqn.basic.utils;

import cn.yqn.basic.jwt.JwtUtils;
import cn.yqn.basic.jwt.Payload;
import cn.yqn.basic.jwt.RsaUtils;
import cn.yqn.user.domain.Logininfo;
import cn.yqn.user.util.LoginData;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/** 
* @Title: 获取登录人信息
* @Description: TODO
* @Author: YQN
* @Version: 1.0
* @Date:  2022/8/9 11:01  
* @Parameters: 
* @Return 
*/
public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            if (token != null) {
                PublicKey publicKey = RsaUtils.getPublicKey(RsaUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                //4.将jwtToken解析成想要的数据：LoginData
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                if (payload != null) {//登录成功，而且没有过期
                    //5.如获取登录信息对象
                    Logininfo logininfo = payload.getLoginData().getLogininfo();
                    return logininfo;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}


